%matplotlib inline
import sys
sys.path.insert(0,'..')
from IPython.display import HTML,Image,SVG,YouTubeVideo
from helpers import header
HTML(header())
from skimage import data
import numpy as np
from skimage.morphology import disk
import skimage.filter.rank as skr
from skimage.measure import label
from skimage.morphology import watershed
from skimage.io import imread
from scipy import ndimage as ndi
import matplotlib.pyplot as plt
from skimage.segmentation import mark_boundaries
/Users/olivier/anaconda2/lib/python2.7/site-packages/skimage/filter/__init__.py:6: skimage_deprecation: The `skimage.filter` module has been renamed to `skimage.filters`. This placeholder module will be removed in v0.13. warn(skimage_deprecation('The `skimage.filter` module has been renamed '
# segment the coins
im = data.coins()
plt.imshow(im,cmap=plt.cm.gray)
plt.colorbar();
# detect the eyes / nose
im = data.chelsea()
plt.imshow(im);
# counting the galaxies
im = data.hubble_deep_field()
plt.imshow(im);
im = data.page()
bg = skr.median(im, disk(10))
res = (1.*im/bg) < .8
plt.imshow(im,cmap=plt.cm.gray)
plt.colorbar();
plt.figure()
plt.imshow(bg,cmap=plt.cm.gray);
plt.colorbar()
plt.figure()
plt.imshow(res,cmap=plt.cm.gray);
plt.colorbar();
Image('http://www.biotechniques.com/multimedia/archive/00003/BTN_A_000112488_O_F00_3051a.jpg')
# segment the cells
im = imread('http://homepages.ulb.ac.be/~odebeir/data/dh_phase.png')
th = im>150
th1 = im>100
plt.imshow(im,cmap=plt.cm.gray)
plt.colorbar();
plt.figure()
plt.imshow(1.*th+th1,cmap=plt.cm.gray)
<matplotlib.image.AxesImage at 0x10aa2a990>
from skimage.filter import canny
ca = canny(im)
plt.figure(figsize=[10,10])
plt.imshow(ca,cmap=plt.cm.gray)
/Users/olivier/anaconda2/lib/python2.7/site-packages/skimage/filter/__init__.py:42: skimage_deprecation: Call to deprecated function ``canny``. Use ``skimage.feature.canny`` instead. def canny(*args, **kwargs):
<matplotlib.image.AxesImage at 0x10f37ae50>
from skimage.morphology import watershed
from skimage.segmentation import mark_boundaries
lab,n_lab = label(th,return_num=True)
bg = th1==0
lab[bg] = n_lab+1
#med = skr.median(im,disk(5))
#gr = skr.gradient(med,disk(3))
ws = watershed(255-im,lab)
plt.imshow(mark_boundaries(im,ws))
<matplotlib.image.AxesImage at 0x10ea31a90>
im = imread('http://homepages.ulb.ac.be/~odebeir/data/exp0001.jpg')
plt.figure(figsize=[20,20])
plt.imshow(im,cmap=plt.cm.gray)
plt.colorbar();
# count red and yellow flowers
im = imread('http://homepages.ulb.ac.be/~odebeir/data/flowers.jpg')
plt.imshow(im)
plt.colorbar();
# find the fiber orientation
im = imread('http://homepages.ulb.ac.be/~odebeir/data/image4.png')
plt.imshow(im,cmap=plt.cm.gray)
plt.colorbar();
from IPython.display import YouTubeVideo
YouTubeVideo('PUcz11MLxUk', start=0, autoplay=1, theme="light", color="blue",)
# detect stroma
im = imread('http://homepages.ulb.ac.be/~odebeir/data/Rp042826d.jpg')
plt.imshow(im,cmap=plt.cm.gray)
plt.colorbar();
# segment the flowers
im = imread('http://homepages.ulb.ac.be/~odebeir/data/KaneFlowers.jpg')
plt.imshow(im,cmap=plt.cm.gray)
plt.colorbar();
from skimage.morphology import watershed
from skimage.segmentation import mark_boundaries
gr = skr.gradient(im,disk(3))
local_min = im <= skr.minimum(im,disk(5))
lab = label(local_min)
#med = skr.median(im,disk(5))
ws = watershed(gr,lab)
plt.figure(figsize=[10,10])
plt.imshow(mark_boundaries(im,ws))
#plt.imshow(local_min)
<matplotlib.image.AxesImage at 0x102ecaf90>
rgb = imread('http://homepages.ulb.ac.be/~odebeir/data/4colors.JPG')
plt.figure(figsize=[20,20])
plt.imshow(rgb)
plt.colorbar();
r = skr.median(rgb[:,:,0],disk(1))
plt.imshow(r,cmap=plt.cm.gray)
<matplotlib.image.AxesImage at 0x10d634950>
s = rgb.sum(axis=2)
th = s > 100
#post-processing
pth = skr.minimum(th.astype(np.uint8),disk(1))
plt.figure(figsize=[20,20])
plt.imshow(pth,cmap=plt.cm.gray)
plt.colorbar()
<matplotlib.colorbar.Colorbar at 0x11a160e10>
lab = label(pth)
lut = np.arange(0,np.max(lab)+1)
plt.imshow(lab)
plt.colorbar()
mask = lab == 20
plt.imshow(mask)
plt.colorbar()
<matplotlib.colorbar.Colorbar at 0x11b99ced0>
from random import shuffle
shuffle(lut)
shuffle(lut)
plt.imshow(lut[lab])
plt.colorbar()
<matplotlib.colorbar.Colorbar at 0x119406e90>
# segment the cell
im = imread('http://homepages.ulb.ac.be/~odebeir/data/exp0001crop.jpg')
plt.imshow(im,cmap=plt.cm.gray)
plt.colorbar();
m = skr.median(im,disk(5))
plt.imshow(m,cmap=plt.cm.gray)
plt.colorbar()
<matplotlib.colorbar.Colorbar at 0x11f88a990>
th1 = m < 90
th2 = np.bitwise_and(110 > m,m < 130)
plt.imshow(th2)
<matplotlib.image.AxesImage at 0x120021bd0>
markers = label(th2)
plt.imshow(markers)
plt.colorbar()
<matplotlib.colorbar.Colorbar at 0x1215b25d0>
markers[markers==3] = 2
ws = watershed(im,markers)
plt.imshow(ws)
plt.imshow(mark_boundaries(im,ws))
<matplotlib.image.AxesImage at 0x1215f8590>
# segment the cell
im = imread('http://homepages.ulb.ac.be/~odebeir/data/brain.jpg')[:,:,0]
plt.figure(figsize=(10,10))
plt.imshow(im,cmap=plt.cm.gray)
plt.colorbar();
plt.hist(im.flatten(),255);
otsu
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-95-84be236c7719> in <module>() ----> 1 otsu NameError: name 'otsu' is not defined
from skimage.filters import threshold_otsu
t_otsu = threshold_otsu(im)
t_otsu
36
th = im > t_otsu
plt.figure(figsize=(10,10))
plt.imshow(th)
<matplotlib.image.AxesImage at 0x121a3f390>
lab = label(th,connectivity=1)
plt.imshow(lab)
<matplotlib.image.AxesImage at 0x11fba3d50>
from skimage.measure import regionprops
props = regionprops(lab)
brain = lab==7
pp = skr.maximum(brain,disk(3))
pp = skr.minimum(pp,disk(3))
plt.imshow(pp)
<matplotlib.image.AxesImage at 0x110eb2610>
for p in props:
print p.area, p.label
1459 1 5 2 1 3 3 4 1 5 16 6 6323 7 1 8 2 9 1 10 1 11 1 12 1 13 16 14 1 15 1 16 1 17 2 18 2 19 2 20 2 21 30 22 1 23 1 24 1 25 1 26 2 27 2 28 5 29 1 30 1 31 2 32 1 33 13 34